package com.smartwork.common.mobile.interceptor;

import com.smartwork.common.config.Global;
import com.smartwork.common.utils.encrypt.Des;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TokenInterceptor extends HandlerInterceptorAdapter {
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

        String env = Global.getConfig("system.environment");

        if ("dev".equals(env) || "test".equals(env)) {
            return true;
        }
		String token = request.getParameter("token");
		String st = request.getParameter("st");
		String uuid = request.getParameter("uuid");
		String clientVersion = request.getParameter("clientVersion");
		String url = request.getRequestURL().toString();
		
		Des des = new Des();
		if (url.equals(des.strDec(token, st, clientVersion, uuid))) {
			return true;
		}
		return false;

	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object o, ModelAndView mav)
			throws Exception {
		System.out.println("postHandle");
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object o, Exception excptn)
			throws Exception {
		System.out.println("afterCompletion");
	}

}
